<template>
<div class="login-wrap">

  <el-form 
  name="form"
  class="login-form"
  :label-position="labelPosition" 
  label-width="80px" 
  :model="formdata">
  <h1>用户登录</h1>
  <el-form-item label="用户名">
    <el-input v-model="formdata.username"></el-input>
  </el-form-item>
  <el-form-item label="密码">
    <el-input v-model="formdata.password"  ></el-input>
  </el-form-item>
  <!-- 按钮 -->
  <el-button 
   @click.prevent="handleLogin()" 
   class="login-btn" 
   type="primary"
   name="btnsubmit"
   >
   登录</el-button>
  
</el-form>
</div>
</template>

<script>
export default {
  data() {
      return {
        labelPosition: 'top',
        formdata: {
          username: 'admin',
          password: '123456',
        }
      };
  },
  methods: {
    //登录请求
    handleLogin() {
      this.$http.post('login',this.formdata).then(res => {
        //  console.log(res)
        const {
          data,
          meta:{ msg , status }
        } = res.data
        // console.log(status)
        if(status === 200) {
          //登陆成功
          //保存token值
          localStorage.setItem('token',data.token)
        // 1.跳转home
          this.$router.push({name:'home'})
          // 2.提示成功
          this.$message.success(msg)
         
        } else{
          // 登录不成功
        // 1.提示错误
        this.$message.warning(msg)
        }

        // // =>
        // const {meta:{msg,status}} =
        // {meta:{msg:'11',status:200}}
        // // =>
        // obj = {msg:'11',status:200}
        // // =>
        // const {msg,status} = {msg:'11',status:200}
        // // =>
        // msg:11 status:200
          
      })
    }
  },
}
</script>

<style>
.login-wrap{
  height: 100%;
  background-color:#324152 ;
  /* 弹性布局  水平垂直居中*/
  display: flex;
  justify-content: center;
  align-items: center; 
}
.login-wrap .login-form{
  width: 380px;
  background-color: #fff;
  border-radius: 5px ;
  padding: 30px;
}
.login-wrap .login-btn{
  width: 100%;
}
</style>